<%@ page language="java" contentType="text/html; charset=windows-1255"
    pageEncoding="windows-1255"%>
<%@ page import="database_layer.*" %>
<%@ page import="java.util.*"%>
<%@ page import="processing_layer.*" %>
<%@ page import="interface_layer.*" %>
<%@ page errorPage="exceptionPage.jsp" %>
<%-- 
	This JSP presents all the users in the system. It presents their avatar (if available) with
	some details along with a link to the user page. It also presents administrative commands for
	the admin, such as deleting users.
 --%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1255"/>
<link href="css/templatemo_style.css" rel="stylesheet" type="text/css" />
<title>Users browser</title>
</head>
<body>
<jsp:include page="header_part_1.html"/>
            <div id="templatemo_menu">
                <ul>
                    <li><a href="index.jsp">Home</a></li>
                    <li><a href="about.jsp">About Us</a></li>
<%
                    	String username = (String) session.getAttribute("username");
                        if (null != username)
                        {
%>
                            <li><a href="userPage.jsp">Your profile</a></li>
		                    <li><a href="allCourses.jsp">Courses</a></li>
		                    <li><a href="allUsers.jsp" class="current">Users</a></li>
<%
                        }
%>
                </ul>    	
            </div> <!-- end of templatemo_menu -->
<jsp:include page="header_part_2.html"/>
<div id="content">
<%
	String userType = (String) session.getAttribute("userType");
	
	if (null == username)
	{
		/* Not logged in = Not authorized */
		ErrorHandler.parseForErrors(Common.UNAUTHORIZED_OPERATION, response, null);
		return;
	}
	
	ArrayList<User> allUsers = QueryDispatcher.getAllUsers();
	
	if (null == allUsers)
	{
		ErrorHandler.parseForErrors(Common.INTERNAL_DB_ERROR, response, null);
		return;
	}
	
	if (allUsers.size() == 0)
	{
		out.println("There are no users yet...");
	}
	
	else
	{
%>
	<table border="1">
<%
		for (User u : allUsers)
		{
%>
			<tr>
				<td>
<%
			if (null == u.photo)
			{
%>
				<img src="images/no-image.jpeg" style="height:100px; width:100px" alt="No photo available"/>            		
<%
			}
			else
			{
%>
				<img src="UserImage?user=<%= u.username %>" style="height:100px; width:100px"/>
<%
			}
%>
				</td>
				<td class="coursesAndUsersTable" id="coursesAndUsersDescription">	
					
<%
			out.println("<b>Username</b>:" + u.username + "<br/>");
			if (u.isAdmin)
			{
				out.println("<b>Role</b>: Administrator<br/>");
			}
			else
			{
				out.println("<b>Role</b>: Student<br/>");
			}
			out.println("<a href=\"userPage.jsp?user=" + u.username + "\">More about this user</a>");
%>
					
				</td>
<%
				if (null != userType)
				{
					if (userType.equals(Common.adminType) && false == u.isAdmin)
					{
%>
				<td class="coursesAndUsersTable" id="coursesAndUsersManagement">	
						<form method="get" action="removeUser">
						<input type="hidden" name="userToRemove" value="<%= u.username %>"/>
						<input type="submit" value="Remove this user" />
						</form>
<%	
					}
				}
%>
				</td>
			</tr>
<%
		}	
	}
%>
	</table>
</div>
<jsp:include page="sidebar.jsp"/>
<jsp:include page="footer.html"/>
</body>
</html>